// Package service 服务包,提供用户相关服务方法
package service

import (
	"fmt"
	"log"

	"gitee.com/daigd/v-blog-demo-go/config"
	"gitee.com/daigd/v-blog-demo-go/model"
)

// QueryUserByUserName 根据用户名查询用户信息
func QueryUserByUserName(userName string) (user model.User, err error) {
	user = model.User{}
	err = config.DB.QueryRow("select user_id,user_name,password,nick_name,created_at,created_by from v_user where user_name = ?", userName).Scan(&user.UserID, &user.UserName, &user.Password, &user.NickName, &user.CreatedAt, &user.CreatedBy)

	return
}

// CheckUserr 根据用户名和密码校验用户信息,如果相等返回true,否则返回false
func CheckUserr(username string, password string) bool {
	user, err := QueryUserByUserName(username)
	if err != nil {
		log.Println(fmt.Sprintf("根据用户名[%s]查询用户记录出现错误", username), err)
		return false
	}
	if user.UserName == username && user.Password == password {
		return true
	}
	return false
}

// QueryUserByID 根据用户Id查询用户
func QueryUserByID(id int) (user model.User, err error) {
	user = model.User{}
	err = config.DB.QueryRow("select user_name,password,nick_name,created_at,created_by from v_user where user_id = ?", id).Scan(&user.UserName, &user.Password, &user.CreatedAt, &user.CreatedBy)
	return
}
